//******************************************************************************
//
// ATM系统 -  Transaction.java 
// 参考了 http://www.cs.gordon.edu/courses/cs211/ATMExample/index.html
// 
//******************************************************************************

package banking;

import atm.ATM;
import atm.Session;

/**
* 这个Transaction类是所有交易类的父类。当用户选择一个交易（取款，存款，查询）时，
* 会实例化相应的交易类。 
* @author  何希
* @version 10/06/2018
*/

public class Transaction {
	
	public static final int TRANS_UNSTART = 1; // 未开始
	
	
	public static final int TYPE_EXIT = 0; // 退出交易
	public static final int TYPE_WITHDRAW = 1; // 取款交易
	public static final int TYPE_DEPOSIT = 2; // 存款交易
	public static final int TYPE_TRANSFER = 3; // 转账交易
	public static final int TYPE_BALANCE = 4; // 查询余额
	
	// 会话的状态
	private int state = TRANS_UNSTART;
	//相关的账户
	private Account acct = null;
	//乙方账户卡号
	private String card_no = null;
	// 对应的对话
	private Session session;
	// 金额的大小
	private double amount = 0.0;
	//交易的类型
	private int type;
	
	/**
	 * 获取金额
	 * @return
	 */
	public double getAmount() {
		return this.amount;
	}
	
	
	
	
	public String getCard_no() {
		return card_no;
	}




	//转账，查询转入卡号是否存在，存在则设置
	public void setAccts(String card_no) {
		
		try {
			//如果账户有效
			if(Account.checkCardNo(card_no)){
				this.card_no = card_no;
				ATM.getInstance().getDisplay().setText("请输入转账金额");
				//开启键盘
				ATM.getInstance().getDigitButton().enterShowMultiInput("TransferInfoServlet");
			}else{
				ATM.getInstance().getDisplay().setText("卡号无效，请重新输入");
				ATM.getInstance().getDigitButton().enterShowMultiInput("CheckTransServlet");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param amount
	 */
	public void setAmount(double amount) {
		this.amount = amount;
	}
	
	public Session getSession() {
		return this.session;
	}
	
	public Account getAccount() {
		return this.acct;
	}
	
	

	public int getState() {
		return this.state;
	}
	
	public void setState(int state) {
		this.state = state;
	}
	
	
	public int getType() {
		return type;
	}

	public Transaction(Session session, Account acct, int type) {
		this.session = session;
		this.acct = acct;
		this.type = type;
	}
	/**
	 * 根据用户的选择，生成一个交易子类
	 * @param session
	 * @param acct
	 * @param options
	 * @return
	 */
	public static Transaction makeTransaction(Session session, Account acct, int options) {
		Transaction tmp = null;
		switch(options) {
			//取款
			case TYPE_WITHDRAW: 
				tmp = new MoneyPump(session,acct,TYPE_WITHDRAW);
				break;
			//存款
			case TYPE_DEPOSIT:
				tmp = new MoneyPump(session, acct,TYPE_DEPOSIT);
				break;
			//转账
			case TYPE_TRANSFER:
				tmp = new MoneyPump(session, acct, TYPE_TRANSFER);
				break;
			//查询余额
			case TYPE_BALANCE:
				tmp = new MoneyPump(session, acct, TYPE_BALANCE);
				break;
			//退卡
			case TYPE_EXIT:
				// 插卡孔状态要变 开关按钮状态要变 显示屏状态要变 数字键盘状态要变
				ATM.getInstance().clear();
				break;
			default: 
				break;
		}
		return tmp;
	}
	
	/**
	 * 取款
	 */
	public void getMoney() {
		System.out.println("取款");
	}
	
	/**
	 * 存款
	 */
	public void putMoney() {
		System.out.println("存款");
	}
	
	/**
	 * 转账
	 */
	public void transMoney(){
		System.out.println("转账");
	}
	
	/**
	 * 处理取款打印
	 * @param flag 0:打印 1:不打印
	 */
	public void withdrawPrint(int flag) {
		
	}
	
	/**
	 * 处理存款打印
	 * @param flag 0:打印 1:不打印
	 */
	public void depositPrint(int flag) {
		
	}
	
	/**
	 * 处理转账打印
	 * @param flag 0:打印 1:不打印
	 */
	public void transPrint(int flag) {
		
	}

	
}
